package com.ymm.system.core.facade;

import com.ymm.system.api.dto.DriverOrderShowDTO;
import com.ymm.system.api.dto.IdInfoDTO;
import com.ymm.system.api.dto.LltorderDetailsDTO;
import com.ymm.system.api.dto.TradeShowDTO;
import com.ymm.system.api.facade.SystemDriverOrderTradeService;
import com.ymm.system.api.utils.OrderStatusEnum;
import com.ymm.system.api.utils.Result;
import com.ymm.system.core.constants.OpsPersonTypeEnum;
import com.ymm.system.core.mq.sender.OrderWaitForConfirmMsgSender;
import com.ymm.system.dao.mapper.LltorderMapper;
import com.ymm.system.dao.mapper.TradeMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


/**
 * @Author zikang.yao
 * @Description TODO
 * @Date 2022/8/4 21:49
 * @Version 1.0
 */
@Service(version = "server")
@Component
@Slf4j
public class SystemDriverOrderTradeServiceImpl implements SystemDriverOrderTradeService {
    @Resource
    private TradeMapper tradeMapper;

    @Resource
    private LltorderMapper lltorderMapper;

    @Resource
    private SystemDisplayServiceImpl systemDisplayServiceImpl;

    @Resource
    private OrderWaitForConfirmMsgSender orderWaitForConfirmMsgSender;


    /**
     * @Author: chencong
     * @Description: 展示具体订单的详情
     * @DateTime: 14:10 2022/8/5
     * @Params: [driverId, tradeId]
     * @Return com.ymm.system.api.dto.TradeShowDTO
     */
    @Override
    public TradeShowDTO getTradeInfo(Long driverId, Long orderId) {
        List<LltorderDetailsDTO> driverIdLltorder = getLltorderDetailsDTO(driverId);
        TradeShowDTO tradeShowDTO = new TradeShowDTO();
        log.info("为什么跑不到啊！！！！！！");
        for (LltorderDetailsDTO driverIdLltorders : driverIdLltorder) {
            if (driverIdLltorders.getOrderId().equals(orderId)) {
                tradeShowDTO = tradeShowDTO(driverIdLltorders);
            }
        }
        log.info(tradeShowDTO.getCargoDes());

        return tradeShowDTO;
    }


    /**
     * @Author: chencong
     * @Description: 拿到该用户展示的所有运单
     * @DateTime: 13:48 2022/8/5
     * @Params: [driverId]
     * @Return java.util.List<com.ymm.system.api.constants.TradeEntity>
     */
    @Override
    public Result<List<DriverOrderShowDTO>> getTradeLists(Long driverId) {
        List<LltorderDetailsDTO> driverIdLltorder = getLltorderDetailsDTO(driverId);
        List<DriverOrderShowDTO> driverIdTrade = new ArrayList<>();
        log.info("Ok?");
        for (LltorderDetailsDTO lltorderDetailsDTO : driverIdLltorder) {
            driverIdTrade.add(driverOrderShowDTO(lltorderDetailsDTO));
        }
        log.info("driverIdTrade:{}", driverIdTrade);
        return Result.success(driverIdTrade);
    }

    /**
     * @Author: chencong
     * @Description: 拿到该用户的所有订单
     * @DateTime: 13:47 2022/8/5
     * @Params: [driverId]
     * @Return java.util.List<com.ymm.system.api.dto.LltorderDetailsDTO>
     */
    @Override
    public List<LltorderDetailsDTO> getLltorderDetailsDTO(Long driverId) {
        List<LltorderDetailsDTO> lltorderDetailsDTOLists = systemDisplayServiceImpl.searchOrderDetails();

        ArrayList<LltorderDetailsDTO> driverIdLltorder = new ArrayList<>();
        for (LltorderDetailsDTO lltorderDetailsDTO : lltorderDetailsDTOLists) {
            if (lltorderDetailsDTO.getDriverId().equals(driverId)) {
                driverIdLltorder.add(lltorderDetailsDTO);
            }
        }
        return driverIdLltorder;
    }


    /**
     * @Author: chencong
     * @Description: 订单卡片显示DTO
     * @DateTime: 13:49 2022/8/5
     * @Params: [lltorderDetailsDTO]
     * @Return com.ymm.system.api.dto.DriverOrderShowDTO
     */
    public DriverOrderShowDTO driverOrderShowDTO(LltorderDetailsDTO lltorderDetailsDTO) {
        DriverOrderShowDTO driverOrderShowDTO = new DriverOrderShowDTO();

        driverOrderShowDTO.setNickname(lltorderDetailsDTO.getShipperEntity().getNickname());
        driverOrderShowDTO.setOrderShipperState(lltorderDetailsDTO.getOrderShipperState());
        driverOrderShowDTO.setTradeId(lltorderDetailsDTO.getTradeId());
        driverOrderShowDTO.setOrderId(lltorderDetailsDTO.getOrderId());
        driverOrderShowDTO.setCargoSrc(lltorderDetailsDTO.getCargoEntity().getCargoSrc());
        driverOrderShowDTO.setCargoDes(lltorderDetailsDTO.getCargoEntity().getCargoDes());
        driverOrderShowDTO.setCargoRequireType(lltorderDetailsDTO.getCargoEntity().getCargoRequireType());
        driverOrderShowDTO.setCargoWeight(lltorderDetailsDTO.getCargoEntity().getCargoWeight());
        driverOrderShowDTO.setCargoDescription(lltorderDetailsDTO.getCargoEntity().getCargoDescription());
        driverOrderShowDTO.setCargoVolume(lltorderDetailsDTO.getCargoEntity().getCargoVolume());

        driverOrderShowDTO.setTradeDriverState(lltorderDetailsDTO.getTradeEntity().getTradeDriverState());
        driverOrderShowDTO.setCargoPrice(lltorderDetailsDTO.getCargoEntity().getCargoPrice());
        driverOrderShowDTO.setOrderCreateDate(lltorderDetailsDTO.getOrderCreateDate());
        return driverOrderShowDTO;
    }

    /**
     * @Author: chencong
     * @Description: 运单详情DTO
     * @DateTime: 13:51 2022/8/5
     * @Params:
     * @Return
     */
    public TradeShowDTO tradeShowDTO(LltorderDetailsDTO lltorderDetailsDTO) {
        TradeShowDTO driverTradeShowDTO = new TradeShowDTO();
        driverTradeShowDTO.setOrderId(lltorderDetailsDTO.getOrderId());

        driverTradeShowDTO.setTradeId(lltorderDetailsDTO.getTradeId());
        driverTradeShowDTO.setTradeDriverState(lltorderDetailsDTO.getTradeEntity().getTradeDriverState());
        driverTradeShowDTO.setOrderShipperState(lltorderDetailsDTO.getOrderShipperState());
        driverTradeShowDTO.setShipperName(lltorderDetailsDTO.getShipperEntity().getNickname());
        driverTradeShowDTO.setShipperPhone(lltorderDetailsDTO.getShipperEntity().getPhoneNumber());
        driverTradeShowDTO.setDriverName(lltorderDetailsDTO.getDriverEntity().getDriverName());
        driverTradeShowDTO.setDriverPhone(lltorderDetailsDTO.getDriverEntity().getDriverPhone());
        driverTradeShowDTO.setDriverType(lltorderDetailsDTO.getDriverEntity().getDriverType());
        driverTradeShowDTO.setDriverCarNum(lltorderDetailsDTO.getDriverEntity().getDriverCarNum());
        driverTradeShowDTO.setOrderCreateDate(lltorderDetailsDTO.getOrderCreateDate());
        driverTradeShowDTO.setTradeArriveTime(lltorderDetailsDTO.getTradeEntity().getTradeArriveTime());
        driverTradeShowDTO.setOrderPrice(lltorderDetailsDTO.getOrderPrice());
        driverTradeShowDTO.setCargoSrc(lltorderDetailsDTO.getCargoEntity().getCargoSrc());
        driverTradeShowDTO.setCargoDes(lltorderDetailsDTO.getCargoEntity().getCargoDes());
        driverTradeShowDTO.setCargoTip(lltorderDetailsDTO.getCargoEntity().getCargoComment());
        driverTradeShowDTO.setDriverCarType(lltorderDetailsDTO.getDriverEntity().getDriverCarType());
        driverTradeShowDTO.setCargoWeight(lltorderDetailsDTO.getCargoEntity().getCargoWeight());
        driverTradeShowDTO.setCargoVolume(lltorderDetailsDTO.getCargoEntity().getCargoVolume());

        return driverTradeShowDTO;
    }


    @Override
    public Result updateTradeState(Long tradeId) {
        if (tradeMapper.getTradeId(tradeId) != null) {
            if (Objects.isNull(tradeMapper.getTradeState(tradeId))) {
                tradeMapper.goCargoLocation(tradeId);
            } else if (tradeMapper.getTradeState(tradeId).equals(0)) {
                tradeMapper.arriveOnCargoLocation(tradeId);
                tradeMapper.updateTradeArriveTime(tradeId);
            } else if (tradeMapper.getTradeState(tradeId).equals(1)) {
                tradeMapper.arriveOffCargoLocation(tradeId);
                tradeMapper.updateTradeLoadTime(tradeId);
            } else if (tradeMapper.getTradeState(tradeId).equals(2)) {
                tradeMapper.finishedOffCargo(tradeId);
            } else if (tradeMapper.getTradeState(tradeId).equals(3)) {
                tradeMapper.finishedTrade(tradeId);
                tradeMapper.updateTradeDischargeTime(tradeId);
                //卸货后向货主后端发送消息
                try {
                    IdInfoDTO idInfo = lltorderMapper.getIdInfoDTOByTradeId(tradeId);
                    orderWaitForConfirmMsgSender.send(idInfo.getOrderId(), idInfo.getShipperId(), idInfo.getCargoId(), OpsPersonTypeEnum.DRIVER.getCode(), idInfo.getDriverId());
                } catch (Exception e){
                    e.printStackTrace();
                    log.error("消息发送失败");
                }
            }
        }
        return Result.success();
    }

    /**
     * @Author: chencong
     * @Description: 拿到该用户进行中的运单列表
     * @DateTime: 13:48 2022/8/5
     * @Params: [driverId]
     * @Return java.util.List<com.ymm.system.api.constants.TradeEntity>
     */
    @Override
    public Result<List<DriverOrderShowDTO>> getTradeProgressLists(Long driverId) {
        List<DriverOrderShowDTO> getdriverProgressOrder = new ArrayList<>();
        List<DriverOrderShowDTO> getdriverAllOrder = getTradeLists(driverId).getData();
        for (DriverOrderShowDTO driverOrderShowDTO : getdriverAllOrder) {
            if (driverOrderShowDTO.getOrderShipperState() == 0) {
                getdriverProgressOrder.add(driverOrderShowDTO);
            }
        }
        return Result.success(getdriverProgressOrder);
    }

    /**
     * @Author: chencong
     * @Description: 拿到该用户历史的运单列表
     * @DateTime: 13:48 2022/8/5
     * @Params: [driverId]
     * @Return java.util.List<com.ymm.system.api.constants.TradeEntity>
     */
    @Override
    public Result<List<DriverOrderShowDTO>> getTradeHistoryLists(Long driverId) {
        List<DriverOrderShowDTO> getdriverHistoryOrder = new ArrayList<>();
        List<DriverOrderShowDTO> getdriverAllOrder = getTradeLists(driverId).getData();
        for (DriverOrderShowDTO driverOrderShowDTO : getdriverAllOrder) {
            if (!driverOrderShowDTO.getOrderShipperState().equals(OrderStatusEnum.ONGOING.getCode())) {
                getdriverHistoryOrder.add(driverOrderShowDTO);
            }
        }

        return Result.success(getdriverHistoryOrder);
    }

}
